package com.example.myapplication.handlerDemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class HandlerPostActivity extends AppCompatActivity implements View.OnClickListener {
    int count = 0;
    ProgressBar progressBar;
    Handler countHandler =  new Handler();
    ///启动Toast,显式线程
    Runnable mRunToast = new Runnable() {
        @Override
        public void run() {
            Toast.makeText(HandlerPostActivity.this,"hello toast",Toast.LENGTH_SHORT).show();
        }
    };
    ///文本区计数线程
    Runnable mRunCount = new Runnable() {
        @Override
        public void run() {
            textView.setText("Count:" + String.valueOf(++count));
            countHandler.postDelayed(mRunCount,1000);
        }
    };

    Handler updateProgressBarHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            log("消息显式内容"+msg.arg1);
            progressBar.setProgress(msg.arg1);
            updateProgressBarHandler.post(mUpdateProgressBarThread);
            super.handleMessage(msg);
        }
    };

    public void log(String str){
        Log.i("TAG", "log: "+str);
    }
    //增加arg1
    Runnable mUpdateProgressBarThread = new Runnable() {
        int i=0;
        @Override
        public void run() {
            log("开启线程："+i);
            i = i+10;
            Message msg = updateProgressBarHandler.obtainMessage();
            msg.arg1 = i;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            updateProgressBarHandler.sendMessage(msg);
        }
    };

    TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_post);
        ((Button)findViewById(R.id.btnShowToast)).setOnClickListener(HandlerPostActivity.this);
        ((Button)findViewById(R.id.btnStart)).setOnClickListener(HandlerPostActivity.this);
        ((Button)findViewById(R.id.btnStop)).setOnClickListener(HandlerPostActivity.this);
        ((Button)findViewById(R.id.btnUpdateBar)).setOnClickListener(HandlerPostActivity.this);
        ((Button)findViewById(R.id.btnStropBar)).setOnClickListener(HandlerPostActivity.this);
        textView = findViewById(R.id.tvCount);
        progressBar = findViewById(R.id.progress_horizontal);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btnShowToast:
                countHandler.postAtTime(mRunToast, SystemClock.uptimeMillis()+5*000);
                break;
            case R.id.btnStart:
                countHandler.postDelayed(mRunCount,1000);
                break;
            case R.id.btnStop:
                countHandler.removeCallbacks(mRunCount);
                break;
            case R.id.btnUpdateBar:
                updateProgressBarHandler.post(mUpdateProgressBarThread);
                break;
            case R.id.btnStropBar:
                updateProgressBarHandler.removeCallbacks(mUpdateProgressBarThread);
                break;
        }
    }
}